package com.jus.player.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.jus.player.R;
import com.jus.player.entity.RechargeDetail;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Objects;

public class RechargeDetailAdapter extends RecyclerView.Adapter<RechargeDetailAdapter.ViewHolder> {

    private List<RechargeDetail> rechargeDetails;
    private final DecimalFormat decimalFormatRecharge = new DecimalFormat("#.00");
    private final DecimalFormat decimalFormatConsume = new DecimalFormat("#");

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_recharge_detail, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RechargeDetail item = rechargeDetails.get(position);
        Context context = holder.itemView.getContext();

        // 设置文本内容
        holder.consumeType.setText(item.getConsumeType());
        holder.symbol.setText(item.getConsumeType().equals("RECHARGE") ? "+" : "-");
        holder.changeUnit.setText(String.valueOf(item.getChangeUnit()));
        holder.operationTime.setText(String.valueOf(item.getOperationTime()));
        holder.balance.setText(String.valueOf(item.getBalance()));

        // 格式化金额
        String formattedAmount = formatAmount(item.getChangeAmount(), item.getConsumeType());
        holder.changeAmount.setText(formattedAmount);

        // 设置颜色
        int textColor = item.getConsumeType().equals("RECHARGE")
                ? ContextCompat.getColor(context, R.color.app_theme_red)
                : ContextCompat.getColor(context, R.color.main_color);
        holder.symbol.setTextColor(textColor);
        holder.changeUnit.setTextColor(textColor);
        holder.changeAmount.setTextColor(textColor);
    }
    private String formatAmount(Double amount, String operation) {
        if (amount == null) return "";
        return "RECHARGE".equals(operation)
                ? decimalFormatRecharge.format(amount)
                : decimalFormatConsume.format(amount);
    }

    @Override
    public int getItemCount() {
        return rechargeDetails == null ? 0 : rechargeDetails.size();
    }

    public void setRechargeDetails(List<RechargeDetail> newDetails) {
        // 使用DiffUtil优化列表更新
        if (rechargeDetails == null) {
            rechargeDetails = newDetails;
            notifyItemRangeInserted(0, newDetails.size());
        } else {
            DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
                @Override
                public int getOldListSize() {
                    return rechargeDetails.size();
                }

                @Override
                public int getNewListSize() {
                    return newDetails.size();
                }

                @Override
                public boolean areItemsTheSame(int oldPos, int newPos) {
                    return Objects.equals(rechargeDetails.get(oldPos).getId(), newDetails.get(newPos).getId());
                }

                @Override
                public boolean areContentsTheSame(int oldPos, int newPos) {
                    return rechargeDetails.get(oldPos).equals(newDetails.get(newPos));
                }
            });
            rechargeDetails = newDetails;
            result.dispatchUpdatesTo(this);
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView consumeType, changeAmount, symbol, changeUnit, operationTime, balance;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            consumeType = itemView.findViewById(R.id.consume_type);
            changeAmount = itemView.findViewById(R.id.change_amount);
            symbol = itemView.findViewById(R.id.symbol);
            changeUnit = itemView.findViewById(R.id.change_unit);
            operationTime = itemView.findViewById(R.id.operation_time);
            balance = itemView.findViewById(R.id.balance);
            // 缓存颜色资源
            int defaultColor = ContextCompat.getColor(itemView.getContext(), R.color.app_theme_red);
            symbol.setTextColor(defaultColor);
            changeUnit.setTextColor(defaultColor);
            changeAmount.setTextColor(defaultColor);
        }
    }
}
